package com.banciyuan.bcywebview.biz.exhibition;

import android.content.Context;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.biz.circles.smooth.CircleTagSmoothActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import java.util.List;

import de.greenrobot.daoexample.model.Exhibition;

/**
 * Created by squall on 2016/8/31.
 */
public class ExhibitionAdapter extends BaseAdapter {

    private List<Exhibition> elist;
    private Context mContext;
    private LayoutInflater inflater;

    public ExhibitionAdapter(List<Exhibition> elist, Context mContext) {
        this.elist = elist;
        this.mContext = mContext;
        this.inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return elist.size();
    }

    @Override
    public Object getItem(int position) {
        return elist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    private class Viewholder {
        public ImageView cover;
        public TextView name;
        public TextView time;
        public TextView address;
        public TextView location;
        public TextView join_count;

        public Viewholder(View view) {
            cover = (ImageView) view.findViewById(R.id.exhibition_iv);
            name = (TextView) view.findViewById(R.id.exhibition_name);
            time = (TextView) view.findViewById(R.id.exhibition_time);
            address = (TextView) view.findViewById(R.id.exhibition_address);
            location = (TextView) view.findViewById(R.id.exhibition_location);
            join_count = (TextView) view.findViewById(R.id.exhibition_join_count);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        Viewholder viewholder = null;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.exhibition_item, null);
            viewholder = new Viewholder(convertView);
            convertView.setTag(viewholder);
        } else {
            viewholder = (Viewholder) convertView.getTag();
        }

        final Exhibition exhibition = elist.get(position);
        if (exhibition != null) {
            ImageLoader.getInstance().displayImage(exhibition.getCover(), viewholder.cover, ImageOptions.options);
            if (!TextUtils.isEmpty(exhibition.getTitle())) {
                viewholder.name.setText(exhibition.getTitle());
            }
            if (!TextUtils.isEmpty(exhibition.getAddress())) {
                viewholder.address.setText(exhibition.getAddress());
            }
            if (!TextUtils.isEmpty(exhibition.getLocation())) {
                viewholder.location.setText(exhibition.getLocation());
            }
            if (!TextUtils.isEmpty(exhibition.getEnd_time()) && !TextUtils.isEmpty(exhibition.getStart_time())) {
                viewholder.time.setText(exhibition.getStart_time() + "~" + exhibition.getEnd_time());
            }
            if (!TextUtils.isEmpty(exhibition.getAttend_count())) {
                viewholder.join_count.setText(Html.fromHtml(String.format(mContext.getString(R.string.attend_unit), exhibition.getAttend_count())));
            }
            ((View) (viewholder.cover.getParent())).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(mContext, CircleTagSmoothActivity.class, exhibition.getTag_name());
                }
            });
        }

        return convertView;
    }

}
